- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRecursion.py
27 lines (24 loc) · 933 Bytes
/
Recursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
classSolution:
defisSubPath(self, head: ListNode, root: TreeNode) ->bool:
ifrootisNone:
returnFalse
elifhead.val==root.valandself.checkPath(head, root):
returnTrue
returnself.isSubPath(head, root.left) orself.isSubPath(head, root.right)
defcheckPath(self, head: ListNode, root: TreeNode) ->bool:
ifheadisNone:
returnTrue
elifrootisNoneorhead.val!=root.val:
returnFalse
returnself.checkPath(head.next, root.left) orself.checkPath(head.next, root.right)